﻿using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.Common;
using SiliconSelect.DAL.DataEntity;
using SiliconSelect.Models;
using SiliconSelect.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SiliconSelect.Views.UserOperatorDialog
{
    /// <summary>
    /// UserEditorWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserAddWindow : Window
    {
        Action? reloadData;
        public UserAddWindow(Action reloadUserData)
        {
            reloadData = reloadUserData;
            InitializeComponent();
        }

        /// <summary>
        /// 点击了保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSaveClick(object sender, RoutedEventArgs e)
        {
            Window window = GetWindow(sender as DependencyObject);
            UserModel? userInfo = window.DataContext as UserModel;
            if (userInfo != null)
            {
                // 如果用户名和密码为空,就提示用户名和密码不能为空
                if (!string.IsNullOrEmpty(userInfo.Username) && !string.IsNullOrEmpty(userInfo.Password))
                {
                    userInfo.CreateBy = GlobalValues.UserInfo!.Role;
                    userInfo.CreateTime = DateTimeHelper.GetTimeNowForSql();
                    userInfo.LastLoginTime = "未登陆";

                    List<string> insertFields = new List<string>()
                    {
                        "Username",
                        "Password",
                        "Role",
                        "CreateBy",
                        "CreateTime",
                        "LastLoginTime"
                    };

                    List<string> insertVals = new List<string>()
                    {
                        userInfo.Username!,
                        userInfo.Password!,
                        userInfo.Role!,
                        userInfo.CreateBy!,
                        userInfo.CreateTime,
                        userInfo.LastLoginTime!
                    };
                    DataResult result = LocalDataAccess.AddUserInfoToDb("Username", userInfo.Username!, insertFields, insertVals);
                    if (result.State)
                    {
                        MessageBox.Show("保存成功");
                        reloadData?.Invoke();
                        window.Close();
                    }
                    else
                    {
                        MessageBox.Show($"保存失败: {result.Message}");
                    }
                }
                else
                {
                    MessageBox.Show($"保存失败: 用户名或者密码不能为空");
                }
            }
        }

        /// <summary>
        /// 点击了取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCancelClick(object sender, RoutedEventArgs e)
        {
            Window window = GetWindow(sender as DependencyObject);
            window.Close();
        }
    }
}
